package com.aifund.app.service.impl.mq;

import com.aifund.app.enums.MqExchangeEnum;
import com.aifund.app.enums.MqRoutingKeyEnum;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Service
public class MqMessageCallBack implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {

    Logger logger = LoggerFactory.getLogger(MqMessageCallBack.class);
    @Autowired
    private RabbitTemplate rabbitTemplate;



    // confirm确认消息投递成功了
    //如果没有 Exchange ack=false,
    //     * 如果有 Exchange ack=tru
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String s) {
        System.out.println("消息发送成功,发送ack确认,id="+correlationData.getId());
        if (ack){
            System.out.println("发送成功");
        }else {
            System.out.println("发送失败");
        }
    }

    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        logger.info("returnedMessage{}",returnedMessage.getMessage());
    }
}
